<?php
namespace jy_newczfee;
defined('BY_JYA') or exit('error');
/*
	分红佣金
	
*/
class commission extends \plugin{
	/*
		生成佣金
	*/
	function create($order,$iscong=0){
		$is_open=intval($this->setting['is_open']);
		if($is_open==0){
			return;
		}
		$this->order=$order;
		if(!$iscong){
			//活动期内是否已经参与新人体验--首次购买产品返虚拟币
			$goods_opentime=$this->setting['goods_opentime']?:0;
			$isfeelog1num=pdo_count('jy_newczfee_feelog',array('createtime >'=>$goods_opentime,'uniacid'=>$_SESSION['uniacid'],'type'=>1,'mid'=>$order['mid']));
			$canfeelog1num=pdo_count('jy_newczfee_order',array('createtime >'=>$goods_opentime,'uniacid'=>$_SESSION['uniacid'],'isnew'=>1,'agentid'=>$order['mid']));
			if($isfeelog1num>=$canfeelog1num+1){
				return;
			}
		}
		if($this->order['price']<=0){
			return;
		}
		$agent_id=pdo_getcolumn('core_members',array('id'=>$this->order['mid'],'uniacid'=>$_SESSION['uniacid']),'fromuser');
		$this->commission_order=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$this->order['mid'],
				'agentid'=>$agent_id,
				'orderid'=>$this->order['id'],
				'price'=>$this->order['price'],
				'plugin_name'=>$this->order['plugin_name']?:'',
				'commission_price'=>$this->order['price'],
				'commission_money'=>0,
				'ispay'=>1,
				'type'=>$iscong,
				'createtime'=>TIMESTAMP,
		);
		if(!$iscong){
			$goodsfee=$this->upfee1();
			if(!$goodsfee){
				return '';
			}
			$paylog=pdo_get('core_paylog',array('id'=>$this->order['pay_id'],'uniacid'=>$_SESSION['uniacid']));
			if($paylog['di_credit']!='credit2' && $paylog['di_fee']>0){
				$goodsfee -=$paylog['di_fee'];
			}
			if($goodsfee<=0){
				return '';
			}
			$this->commission_order['commission_price']=$goodsfee;
		}
		//转介绍
		if($this->setting['jie_open'] && !$iscong){
			$this->commission_order['isnew']=1;
			$cong_levels=$this->setting['cong_levels'];
			if($cong_levels && $agent_id){
				$groupid=pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$agent_id),'groupid');
				if(strpos($cong_levels,';'.$groupid.';')===false){
					$this->commission_order['isnew']=2;
				}
			}
			
		}
		//充值返
		if($this->setting['cong_open'] && $iscong){
			$monthfeeset=$this->setting['monthfeeset'];
			$monthfee=0;
			paixu($monthfeeset,'man','desc');
			foreach($monthfeeset as $gv){
				if($gv['man']<=$this->order['price']){
					$monthfee=$gv['fee'];
					break;
				}
			}
			$this->commission_order['month']=$this->setting['cf_month'];
			$this->commission_order['lastmonth']=$this->setting['cf_month'];
			$this->commission_order['monthfee']=$monthfee;
		}
		//生成分红订单
		$indata=$this->commission_order;
		$this->insert('order',$indata);
		return true;
	}
	public function upfee1(){
		$goods_ids=$this->setting['goods_ids'];
		$goods_ids2=array();
		foreach($goods_ids as $gv){
			$goods_ids2[]=$gv['id'];
		}
		$levelid=pdo_getcolumn('jy_weishop_member',array('mid'=>$this->order['mid'],'merchid'=>0,'uniacid'=>$_SESSION['uniacid']),'level');
		$goodstiset=$this->setting['goodstiset'];
		foreach($goodstiset as $gv){
			if($gv['levelid']==$levelid){
				$cannum=$gv['num'];
				break;
			}
		}
		$goodsnum=0;
		$goodsfee=0;
		//file_put_contents(ROOT_D.'/2.txt',$goods['goodsid'].'~~'.json_encode($goods_ids2));
		$goodslist=$this->order['goods'];
		paixu($goodslist,'price','desc');
		foreach($goodslist as $goods){
			if(in_array($goods['goodsid'],$goods_ids2)){
				if($goodsnum+$goods['total']>$cannum){
					$goods['total']=$cannum-$goodsnum;
				}
				$goodsnum +=$goods['total'];
				$goodsfee +=$goods['total']*$goods['price'];
				if($goodsnum>=$cannum){
					break;
				}
			}
		}
		$goodsfee=$goodsfee>$this->order['price']?$this->order['price']:$goodsfee;
		//file_put_contents(ROOT_D.'/1.txt',$goodsfee.'~~'.$goodsnum.'~~'.$cannum);
		if($goodsfee<=0 || !$goodsnum || $goodsnum>$cannum){
			return '';
		}
		if($goodsfee){
			$indata=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$this->order['mid'],
					'orderid'=>$this->order['id'],
					'levelid'=>$levelid,
					'fee'=>$goodsfee,
					'type'=>1,
					'createtime'=>TIMESTAMP,
			);
			$this->insert('feelog',$indata);
		}
		return $goodsfee;
	}
	/*
		订单完成时，把佣金状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		pdo_update('jy_newczfee_feelog',array('status'=>2,'complete_time'=>TIMESTAMP),array('type'=>1,'status'=>0,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		$feelogs=pdo_getall('jy_newczfee_feelog',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		foreach($feelogs as $fv){
			model('core/member')->member_credit($fv['mid'],'credit3',$fv['fee'],true,'jy_newczfee','返'.$this->b3title);
		}
		return true;
	}
	/*
		取消订单
	*/
	public function cancel($order){
		pdo_update('jy_newczfee_feelog',array('status'=>4,'complete_time'=>TIMESTAMP),array('type'=>1,'status'=>0,'uniacid'=>$_SESSION['uniacid'],'orderid'=>$order['id']));
		return true;
	}
}